home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C10 / Depend.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  42 lines

  1. //: C10:Depend.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Static initialization technique
  7. #ifndef DEPEND_H
  8. #define DEPEND_H
  9. #include <iostream>
  10. extern int x; // Declarations, not definitions
  11. extern int y;
  12.  
  13. class Initializer {
  14.   static int init_count;
  15. public:
  16.   Initializer() {
  17.     std::cout << "Initializer()" << std::endl;
  18.     // Initialize first time only
  19.     if(init_count++ == 0) {
  20.       std::cout << "performing initialization"
  21.            << std::endl;
  22.       x = 100;
  23.       y = 200;
  24.     }
  25.   }
  26.   ~Initializer() {
  27.     std::cout << "~Initializer()" << std::endl;
  28.     // Clean up last time only
  29.     if(--init_count == 0) {
  30.       std::cout << "performing cleanup" 
  31.         << std::endl;
  32.       // Any necessary cleanup here
  33.     }
  34.   }
  35. };
  36.  
  37. // The following creates one object in each
  38. // file where DEPEND.H is included, but that
  39. // object is only visible within that file:
  40. static Initializer init;
  41. #endif // DEPEND_H ///:~
  42.